package com.chang.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.reggie.common.CustomException;
import com.chang.reggie.entity.Category;
import com.chang.reggie.entity.Dish;
import com.chang.reggie.entity.Setmeal;
import com.chang.reggie.mapper.CategoryMapper;
import com.chang.reggie.service.CategoryService;
import com.chang.reggie.service.DishService;
import com.chang.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Chang
 * @date 2022-09-04 13:53
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        // 创建条件构造器
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        // 查询当前分类是否关联菜品
        int dishs = dishService.count(dishLambdaQueryWrapper);
        // 如果已经关联，抛出业务异常
        if (dishs > 0) {
            throw new CustomException("已经关联菜品，不能删除");
        }
        // 创建条件构造器
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        // 查询当前分类是否关联套餐
        int setmeals = setmealService.count(setmealLambdaQueryWrapper);
        // 如果已经关联，抛出业务异常
        if (setmeals > 0) {
            throw new CustomException("已经关联套餐，不能删除");
        }
        super.removeById(id);
    }

}
